//-------------------------------------------------------------------------
/**
   File:   Ip4.h

   Descr.: Wrapper for IPv4 addresses.

   Author: Stefan Leihkauf
   Date:   23.03.2014
*/
//-------------------------------------------------------------------------
#ifndef _IP4_H
#define _IP4_H
#include "common/types/typedef.h"

namespace cb { namespace common { namespace net {

class Ip4
{
    union
    {
        struct
        {
            uchar c0;
            uchar c1;
            uchar c2;
            uchar c3;
        };

        ulong value;
    } _ip;

public:
    /// constructors
    Ip4();
    Ip4(uchar c0, uchar c1, uchar c2, uchar c3);
    Ip4(ulong value);

    /// set ip4 address
    void set(uchar c0, uchar c1, uchar c2, uchar c3);
    void set(ulong value);
    void set(const StdString& str);

    /// clear
    void clear();

    /// check if empty
    bool isEmpty() const;

    /// convert to string
    std::string toString() const;

    /// convert to unsigned long
    ulong toULong() const;

    /// access bytes
    uchar& operator [] (uint idx);
    const uchar& operator [] (uint idx) const;

    /// equality
    bool operator ==(const Ip4& ip) const;
    bool operator !=(const Ip4& ip) const;

    /// create from string
    static Ip4 fromString(const StdString& strIp);

    /// empty type
    static Ip4 Empty();
};

}}} // namespace

#endif //!defined _IP4_H